﻿define(function (require, exports, module) {
    return function (jQuery) {
        (function ($) {
            var dateFormat = function (date, fmt) { //author: meizz
                var o = {
                    "M+": date.getMonth() + 1,                 //月份
                    "d+": date.getDate(),                    //日
                    "h+": date.getHours(),                   //小时
                    "m+": date.getMinutes(),                 //分
                    "s+": date.getSeconds(),                 //秒
                    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                    "S": date.getMilliseconds()             //毫秒
                };
                if (/(y+)/.test(fmt))
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt))
                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            };
            var defaulttime = new Date(new Date().setDate(new Date().getDate() - 1));
            //提交下载任务
            var submitDownloadTask = function (postdata,posturl) {
                $.ajax({
                    url: posturl,
                    type: "POST",
                    data: JSON.stringify(postdata),
                    dataType: "json",
                    success: function (result, textStatus) {
                        if(result.state){
                            alert('提交下载任务成功!');
                        }
                        else{
                            alert("提交下载任务失败,\r\n"+result.error);
                        }
                    },
                    error:function(xmlHttpRequest,textStatus){
                        alert("请求出错!");
                    },
                    complete:function(xmlHttpRequest,textStatus){

                    }
                });
            };
            var terminalHotCtrl = function ($scope, httpservice) {
                var formatterOperate = function(cellvalue, options, rowObject){
                    var listprint = "<button class='serviceTypebtn terUerDetail' style='width:90px;height:20px;' search_starttime='"+rowObject.search_starttime+"' search_endtime='"+rowObject.search_endtime+"' type='"+rowObject.type+"' brand='"+rowObject.brand+"' data-id='" + rowObject.servicetypecode + "'>导出用户明细</button>";
                    return listprint;
                };

                var downloadoperate = function(cellvalue, options, rowObject){
                    var listprint = "<a class='viewmodel' href='javascript:'id='"+rowObject.id+"' title = '查看'><img src='../../../images/chakan.png' /></a>" +
                        "<a class='downloadmodel' style='margin: 0 15px;' href= '"+httpservice.downloadurl+""+rowObject.filename+".csv' target='_blank' id='"+rowObject.id+"' state='"+rowObject.status+"' title='下载'><img src='../../../images/download.png' /></a>" +
                        "<a class='deletemodel' href='javascript:' id='"+rowObject.id+"' state='"+rowObject.state+"' title='删除'><img src='../../../images/delete1.png' /></a>";
                    return listprint;
                };

                $scope.grid1 = {
                    gridpagedata:{
                        nowpage:1,
                        totalpage:1,
                        totalcount:20,
                        startline:1,
                        endline:20,
                        pagesize:20,
                        datacount:20,
                        pagelist:[20,50,100],
                        //第一页
                        toFirstpage: function(){
                            $scope.grid1.gridpagedata.nowpage=1;
                            $scope.turnPage1();
                        },
                        //上一页
                        toToppage: function(){
                            if($scope.grid1.gridpagedata.nowpage>1){
                                $scope.grid1.gridpagedata.nowpage--;
                            }
                            $scope.turnPage1();
                        },
                        //下一页
                        toNextpage: function(){
                            if($scope.grid1.gridpagedata.nowpage<$scope.grid1.gridpagedata.totalpage){
                                $scope.grid1.gridpagedata.nowpage++;
                            }
                            $scope.turnPage1();
                        },
                        //最后一页
                        toLastpage: function(){
                            $scope.grid1.gridpagedata.nowpage = $scope.grid1.gridpagedata.totalpage;
                            $scope.turnPage1();
                        },
                        //跳转到某一页
                        topage: function(){
                            if($scope.grid1.gridpagedata.nowpage<1)
                            {
                                $scope.grid1.gridpagedata.nowpage = 1;
                            }
                            if($scope.grid1.gridpagedata.nowpage>$scope.grid1.gridpagedata.totalpage){
                                $scope.grid1.gridpagedata.nowpage = $scope.grid1.gridpagedata.totalpage;
                            }
                            $scope.turnPage1();
                        },
                        //改变页面显示记录数
                        changePagesize: function(){
                            $scope.grid1.gridpagedata.nowpage = 1;
                            $scope.turnPage1();
                        }
                    },
                    griddata: {
                        colnames: ['起始时间','终止时间','排名','型号','品牌','操作系统','使用人数',"操作"],
                        colmodel: [
                            { name: 'search_starttime', index: 'search_starttime', sortable: false,  align:"center",hidden:true},
                            { name: 'search_endtime', index: 'search_endtime', sortable: false,  align:"center",hidden:true},
                            { name: 'ranking', index: 'ranking', sortable: false, sorttype: 'number', align:"center"},
                            { name: 'type', index: 'type', sortable: false, align:"center"},
                            { name: 'brand', index: 'brand', sortable: true, align:"center"},
                            { name: 'os', index: 'os', sortable: true, align:"center"},
                            { name: 'imsi_count', index: 'imsi_count', sortable: true, sorttype: 'number',align:"center"},
                            { name: 'operate', index: 'operate', sortable: false, align:"center",formatter:formatterOperate}
                        ],
                        data: [
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10", ranking: "1",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000},
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10", ranking: "2",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000},
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10", ranking: "3",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000},
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10",ranking: "4",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000},
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10",ranking: "5",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000},
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10",ranking: "6",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000},
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10",ranking: "7",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000},
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10",ranking: "8",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000},
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10",ranking: "9",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000},
//                            { search_starttime:"2015-10-10",search_endtime:"2015-10-10",ranking: "10",type: "iPhone6S", brand: "苹果", os:"IOS", "imsi_count":1000000}
                        ]
                    },
                    obj:{},
                    shrinkToFit:true,
                    gridresize: function (grid) {
                        grid.setGridWidth($(window).width());
                        grid.setGridHeight($(window).height()-260);
                    }
                };

                //品牌和OS关联
                $("#brand").live('change',function(){
                    if($("#brand").val() == "苹果"){
                        $("#os").val("IOS");
                        $scope.$apply();
                    }else if($("#brand").val() == ""){
                        $("#os").val("--");
                        $scope.$apply();
                    }else{
                        $("#os").val("ANDROID");
                        $scope.$apply();
                    }
                });

                $("#os").live('change',function(){
                    if($("#os").val() == "IOS"){
                        $("#brand").empty();
                        $("#brand").append("<option value='苹果'>苹果</option>");
                        $scope.$apply();
                    }else if($("#os").val() == ""){
                        $("#brand").empty();
                        //$("#brand").append("<option value='' >--</option>");
                        //$("#brand").append("<option value='APPLE'>苹果</option>");
                        //$("#brand").append("<option value='SAMSUNG'>三星</option>");
                        //$("#brand").append("<option value='Meizu'>魅族</option>");
                        //$("#brand").append(" <option value='Xiaomi'>小米</option>");
                        $("#brand").append("<option value=''>--</option>")
                        $("#brand").append("<option value='苹果'>苹果</option>");
                        $("#brand").append("<option value='三星'>三星</option>");
                        $("#brand").append("<option value='魅族'>魅族</option>");
                        $("#brand").append("<option value='小米'>小米</option>");
                        $("#brand").append("<option value='华为'>华为</option>");
                        $("#brand").append("<option value='联想'>联想</option>");
                        $("#brand").append("<option value='OPPO'>OPPO</option>");
                        $("#brand").append("<option value='VIVO'>VIVO</option>");
                        $("#brand").append("<option value='中兴'>中兴</option>");
                        $scope.$apply();
                    }else{
                        $("#brand").empty();
                        //$("#brand").append("<option value='' >--</option>");
                        //$("#brand").append("<option value='SAMSUNG'>三星</option>");
                        //$("#brand").append("<option value='Meizu'>魅族</option>");
                        //$("#brand").append(" <option value='Xiaomi'>小米</option>");
                        $("#brand").append("<option value=''>--</option>")
                        $("#brand").append("<option value='三星'>三星</option>");
                        $("#brand").append("<option value='魅族'>魅族</option>");
                        $("#brand").append("<option value='小米'>小米</option>");
                        $("#brand").append("<option value='华为'>华为</option>");
                        $("#brand").append("<option value='联想'>联想</option>");
                        $("#brand").append("<option value='OPPO'>OPPO</option>");
                        $("#brand").append("<option value='VIVO'>VIVO</option>");
                        $("#brand").append("<option value='中兴'>中兴</option>");
                        $scope.$apply();
                    }
                });

                //获得终端热点分析表数据
                var getTerHot = function(){
                    if($("#starttime1").val() != ''){
                        if($("#endtime1").val() != ''){
                            if($("#starttime1").val() <= $("#endtime1").val()){
                                $scope.$emit('loading', 'true');
                                var terGroupCondition = {
                                    starttime:$("#starttime1").val(),
                                    endtime:$("#endtime1").val(),
                                    os:$("#os").val(),
                                    brand:$("#brand").val(),
                                    pageindex:$scope.grid1.gridpagedata.nowpage,
                                    pagesize:$scope.grid1.gridpagedata.pagesize
                                };
                                // alert($("#os1").val());
                                var getTH = httpservice.javaPost("terminalHot.do?method=showgrid",terGroupCondition,function(result){
                                    $scope.grid1.gridpagedata.totalcount = result.recordCount;
                                    $scope.grid1.gridpagedata.totalpage = result.totalPage;
                                    $scope.grid1.gridpagedata.startline =result.startLine;
                                    $scope.grid1.gridpagedata.endline = result.endLine;
                                    $scope.grid1.gridpagedata.datacount = result.dataCount;
                                    $scope.grid1.griddata.data = result.datas;

                                    $scope.$apply();

                                });
                                $.when(getTH).then(function () {
                                }).always(function () {
                                        $scope.$emit('loading', 'false');
                                        $scope.$apply();
                                    });
                            }else{
                                alert("起始时间不能大于终止时间！");
                            }

                        }else{
                            alert("请输入查询终止的时间!");
                        }
                    }else{
                        alert("请输入查询的起始时间!");
                    }

                };

                //导出终端热点分析表数据
                $scope.csvDownload1 = function(){
                    if($scope.grid1.griddata.data != ''){
                        var starttime = $("#starttime1").val();
                        var endtime = $("#endtime1").val();
                        var os = $("#os").val();
                        var brand = $("#brand").val();
                        // window.open(httpservice.javaserverurl+"terminalHot.do?method=csvDownload");
                        window.location.href = httpservice.javaserverurl+"terminalHot.do?method=csvDownload&starttime="+starttime+"&endtime="+endtime+"&os="+os+"&brand="+brand;
                    }else{
                        alert("无数据，请先进行查询！");
                        return false;
                    }
                };

                //提交导出终端热点分析表数据的导出任务
                $scope.exportTermHotPointTask = function(){
                    if($scope.grid1.griddata.data != ''){
                        var condition = {
                            starttime : $("#starttime1").val(),
                            endtime : $("#endtime1").val(),
                            os : $("#os").val(),
                            brand : $("#brand").val(),
                            mainmodule:"终端多维分析",
                            submodule:"终端热点分析",
                            appname:"终端热点分析"
                        }
                        var url = httpservice.serverurl + "script/Terminal/SubmitTermHotPointTask";
                        submitDownloadTask(condition,url);
                    }else{
                        alert("无数据，请先进行查询！");
                        return false;
                    }
                }
                //导出终端用户明细表
              /*  $(".terUerDetail").live("click",function(){
                    window.location.href = httpservice.javaserverurl+"terminalHot.do?method=terDetailDownload&search_starttime="+$(this).attr("search_starttime")+"&search_endtime="+$(this).attr("search_endtime")+"&type="+$(this).attr("type")+"&brand="+$(this).attr("brand");
                });*/

                //提交导出终端热点用户明细的导出任务
                $(".terUerDetail").live("click",function(){
                    var condition = {
                        starttime:$(this).attr("search_starttime"),
                        endtime:$(this).attr("search_endtime"),
                        brand:$(this).attr("brand"),
                        type:$(this).attr("type"),
                        mainmodule:"终端多维分析",
                        submodule:"终端热点分析",
                        appname:"终端热点分析"
                    }
                    var url = httpservice.serverurl + "script/Terminal/SubmitTermHotPointUserDetailTask";
                    submitDownloadTask(condition,url);
                });

                //查询
                $scope.queryTerHot = function(){
                    $scope.grid1.gridpagedata.nowpage = 1;
                    $scope.grid1.gridpagedata.pagesize = 20;
                    $scope.$apply();
                    getTerHot();
                };

                $scope.quicklyQueryTerHot = function(value){
                    var num = value;

                    $("#starttime1").val(dateFormat(new Date(new Date().setDate(new Date().getDate() -num)),'yyyy-MM-dd'));
                    $("#endtime1").val(dateFormat(new Date(new Date().setDate(new Date().getDate() -1)),'yyyy-MM-dd'));

                    getTerHot();
                };
                //翻页
                $scope.turnPage1 = function(){
                    getTerHot();
                }

                $scope.dialogList={
                    title:"下载列表",
                    ok:function(){},
                    cancel:function(){}
                };

                $scope.downloadList = {
                    griddata: {
                        colnames: ['ID','下载文件名', '创建时间','主模块','子模块','应用名称', '下载状态', '操作'],
                        colmodel: [
                            { name: 'id', index: 'id', sortable: false, align:"center",hidden:true },
                            { name: 'filename', index: 'filename', sortable: false, align:"center" },
                            { name: 'createtime', index: 'createtime', sortable: false, align:"center" },
                            { name: 'mainmodule',width:100, index: 'mainmodule', sortable: false, align:"center"},
                            { name: 'submodule',width:100, index: 'submodule', sortable: false, align:"center"},
                            { name: 'appname',width:100, index: 'appname', sortable: false,sorttype: 'number', align:"center"},
                            { name: 'status',width:100, index: 'status', sortable: false,sorttype: 'number', align:"center"},
                            { name: 'operate', index: 'operate', sortable: false, align:"center",formatter:downloadoperate}
                        ],
                        data: [
                            { "filename": "微信", "mainmodule": 1000, "submodule":1000, "appname":1000, "createtime": 1000,"status":1000, "operate":1000}
                        ]
                    },
                    obj:{},
                    shrinkToFit:true,
                    gridresize: function (grid) {
                        grid.setGridWidth(800);
                        grid.setGridHeight(300);
                    }
                };

                $scope.showDownloadlist = function(){
                    $scope.dialogList.show();
                    $scope.getTermHotPointDownloadList();
                }

                $scope.getDownloadList = function(postdata){
                    $.ajax({
                        url:httpservice.serverurl + "script/Common/GetTaskList",
                        type:"post",
                        data:JSON.stringify(postdata),
                        dataType:"json",
                        success:function(result){
                            $scope.downloadList.griddata.data = result.datas;
                            $scope.$apply();
                        },
                        error:function(xmlHttpRequest,textStatus){
                            //alert("请求出错!");
                        },
                        complete:function(xmlhHttpRequest,textStatus){

                        }
                    });
                }

                $scope.getTermHotPointDownloadList = function(){
                    var condition = {
                        mainmodule:"终端多维分析",
                        submodule:"终端热点分析"
                    }
                    $scope.getDownloadList(condition);
                }

                //初始化
                $scope.init = function(){
                    $scope.getTermHotPointDownloadList();
                    setInterval(function(){
                        $scope.$apply($scope.getTermHotPointDownloadList);
                    }, 60*1000);
                };

                $scope.init();
            };
            var terminalBusCtrl = function ($scope, httpservice){
                var isini = "init";
                var reccount = 0;
                $scope.brand="";
                $scope.starttime="";
                $scope.endtime="";
                var down=false;
                var formatterOperate1 = function(cellvalue, options, rowObject){
                    var listprint = "<button class='serviceTypebtn' style='width:90px;height:20px;'  subservice='" + rowObject.subservice + "' brand='"+rowObject.brand+"' type='" + rowObject.model + "' endtime='"+rowObject.endtime+"' starttime='"+rowObject.starttime+"' network='"+rowObject.network+"'>导出用户明细</button>";
                    return listprint;
                };

                var downloadoperate = function(cellvalue, options, rowObject){
                    var listprint = "<a class='viewmodel' href='javascript:'id='"+rowObject.id+"' title = '查看'><img src='../../../images/chakan.png' /></a>" +
                        "<a class='downloadmodel' style='margin: 0 15px;' href= '"+httpservice.downloadurl+""+rowObject.filename+".csv' target='_blank' id='"+rowObject.id+"' state='"+rowObject.status+"' title='下载'><img src='../../../images/download.png' /></a>" +
                        "<a class='deletemodel' href='javascript:' id='"+rowObject.id+"' state='"+rowObject.state+"' title='删除'><img src='../../../images/delete1.png' /></a>";
                    return listprint;
                };

                $scope.grid1 = {
                    gridpagedata:{
                        nowpage:1,
                        totalpage:1,
                        totalcount:20,
                        startline:1,
                        endline:20,
                        pagesize:20,
                        datacount:20,
                        pagelist:[20,50,100],
                        //第一页
                        toFirstpage: function(){
                            $scope.grid1.gridpagedata.nowpage=1;
                            $scope.turnPage1();
                        },
                        //上一页
                        toToppage: function(){
                            if($scope.grid1.gridpagedata.nowpage>1){
                                $scope.grid1.gridpagedata.nowpage--;
                            }
                            $scope.turnPage1();
                        },
                        //下一页
                        toNextpage: function(){
                            if($scope.grid1.gridpagedata.nowpage<$scope.grid1.gridpagedata.totalpage){
                                $scope.grid1.gridpagedata.nowpage++;
                            }
                            $scope.turnPage1();
                        },
                        //最后一页
                        toLastpage: function(){
                            $scope.grid1.gridpagedata.nowpage = $scope.grid1.gridpagedata.totalpage;
                            $scope.turnPage1();
                        },
                        //跳转到某一页
                        topage: function(){
                            if($scope.grid1.gridpagedata.nowpage<1)
                            {
                                $scope.grid1.gridpagedata.nowpage = 1;
                            }
                            if($scope.grid1.gridpagedata.nowpage>$scope.grid1.gridpagedata.totalpage){
                                $scope.grid1.gridpagedata.nowpage = $scope.grid1.gridpagedata.totalpage;
                            }
                            $scope.turnPage1();
                        },
                        //改变页面显示记录数
                        changePagesize: function(){
                            $scope.grid1.gridpagedata.nowpage = 1;
                            $scope.turnPage1();
                        }
                    },
                    griddata: {
                        colnames: ['序号','终端型号','终端品牌','二级','开始','结束','业务偏好','网络（2/3/4G）','使用流量（单位/Mb）',"操作"],
                        colmodel: [
                            { name: 'number', index: 'number', sortable: false, sorttype: 'number', align:"center"},
                            { name: 'model', index: 'model', sortable: false, align:"center"},
                            { name: 'brand', index: 'brand', sortable: true, align:"center"},
                            { name: 'subservice', index: 'subservice', sortable: true, sorttype: 'number',align:"center",hidden:true},
                            { name: 'starttime', index: 'starttime', sortable: true, sorttype: 'number',align:"center",hidden:true},
                            { name: 'endtime', index: 'endtime', sortable: true, sorttype: 'number',align:"center",hidden:true},
                            { name: 'business', index: 'business', sortable: true, sorttype: 'number',align:"center"},
                            { name: 'network', index: 'network', sortable: true, align:"center"},
                            { name: 'amonthtraffic', index: 'amonthtraffic', sortable: true, align:"center"},
                            { name: 'operate', index: 'operate', sortable: false, align:"center",formatter:formatterOperate1}
                        ],
                        data: [
//                            { number:"1",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"},
//                            { number:"2",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"},
//                            { number:"3",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"},
//                            { number:"4",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"},
//                            { number:"5",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"},
//                            { number:"6",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"},
//                            { number:"7",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"},
//                            { number:"8",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"},
//                            { number:"9",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"},
//                            { number:"10",model:"iPhone6S", brand:"苹果", business:"", network:"4G", "amonthtraffic":"500M"}
                        ]
                    },
                    obj:{},
                    shrinkToFit:true,
                    gridresize: function (grid) {
                        grid.setGridWidth($(window).width());
                        grid.setGridHeight($(window).height()-235);
                    }
                };
                $scope.turnPage1 = function(){
                    $scope.getterminalLen($scope.getturnpagecondition());

                };
                $scope.getrecentconditon = function(reccount){
                    var condition = { nowpage:$scope.grid1.gridpagedata.nowpage,
                        pagesize:$scope.grid1.gridpagedata.pagesize,
                        starttime: dateFormat(new Date(new Date().setDate(new Date().getDate() -reccount)),'yyyy-MM-dd'),
                        endtime: dateFormat(new Date(new Date().setDate(new Date().getDate())),'yyyy-MM-dd'),
                        endtime1: dateFormat(new Date(new Date().setDate(new Date().getDate()-1)),'yyyy-MM-dd')

                    };
                    return condition;
                };
                $scope.selectQuery1=function(recentday){
                    isini = "recent";
                    reccount  =recentday;
                    $scope.grid1.gridpagedata.nowpage=1;
                    $("#brand").find("option[value='']").attr("selected",true);
                    var condition = $scope.getrecentconditon(recentday);
                    $("#starttime1").val(condition.starttime);
                    $("#endtime1").val(condition.endtime1);
                    $scope.getterminalLen(condition);
                };
                $scope.getinitcondition = function(){
                    var condition = {
                        nowpage:$scope.grid1.gridpagedata.nowpage,
                        pagesize:$scope.grid1.gridpagedata.pagesize,
                        starttime:dateFormat(new Date(new Date().setDate(new Date().getDate() -1)),'yyyy-MM-dd'),
                        endtime:dateFormat(new Date(new Date().setDate(new Date().getDate())),'yyyy-MM-dd')
                    };
                    return condition;
                };
                $scope.getquerycondition = function(){
                    var end=Date.parse($("#endtime1").val());
                    var enddate=new Date(end);
                    var temp = enddate.valueOf()+1*24*60*60*1000;
                    var condition = {
                        nowpage:$scope.grid1.gridpagedata.nowpage,
                        pagesize:$scope.grid1.gridpagedata.pagesize,
                        brand:$("#brand").val(),
                        starttime:$("#starttime1").val(),
                        endtime:dateFormat(new Date(temp),'yyyy-MM-dd')
                    };
                    return condition;

                };
                $scope.getturnpagecondition = function(){
                    var condition;
                    if(isini=="init"){
                        condition = $scope.getinitcondition();
                    }else if(isini=="recent"){
                        condition = {nowpage:$scope.grid1.gridpagedata.nowpage,
                            pagesize:$scope.grid1.gridpagedata.pagesize,
                            starttime: dateFormat(new Date(new Date().setDate(new Date().getDate() -reccount)),'yyyy-MM-dd'),
                            endtime: dateFormat(new Date(new Date().setDate(new Date().getDate())),'yyyy-MM-dd')
                        }
                    }else if(isini=="query"){
                        condition = $scope.getquerycondition();
                    }
                    return condition;
                };
                $scope.checkdate = function(){
                    var start = $("#starttime1").val();
                    var end = $("#endtime1").val();
                    if(start==""||end==""){
                        return false;
                    }else{
                        return true;
                    }

                };
                $scope.queryTerminallen = function(){
                    if($scope.checkdate()){
                        isini = "query";
                        $scope.grid1.gridpagedata.nowpage=1;
                        $scope.getterminalLen($scope.getquerycondition());
                    }else{
                        alert("请输入开始时间和结束时间");
                    }

                };
                $scope.getinitdate = function(){
                    var date={
                        startdate:dateFormat(new Date(new Date().setDate(new Date().getDate() -1)),'yyyy-MM-dd'),
                        enddate:dateFormat(new Date(new Date().setDate(new Date().getDate()-1)),'yyyy-MM-dd')
                    };
                    /* $("#starttime1").val(dateFormat(new Date(new Date().setDate(new Date().getDate() -10)),'yyyy-MM-dd'));
                     $("#endtime1").val(dateFormat(new Date(new Date().setDate(new Date().getDate())),'yyyy-MM-dd'));*/
                    return date;
                };
                $scope.download = function(){
                    if(down==true){
                        var url = "terminalLen.do?method=exportexcel&brand="+$scope.brand+"&starttime="+$scope.starttime+"&endtime="+$scope.endtime;
                        var downurl =httpservice.javaserverurl+url;
                        window.location.href=downurl;
                    }else{
                        alert("无数据下载")
                    }
                };

                /*$(".serviceTypebtn").live("click",function(){
                    var conditon={
                        type:$(this).attr("type"),
                        network:$(this).attr("network"),
                        subservice:$(this).attr("subservice"),
                        starttime:$(this).attr("starttime"),
                        endtime:$(this).attr("endtime")
                    };
                    var url = "terminalLen.do?method=exportServiceDetail";
                    var location = httpservice.javaserverurl+url+"&type="+conditon.type+"&network="+conditon.network+"&subservice="+conditon.subservice+"&starttime="+conditon.starttime+"&endtime="+conditon.endtime;
                    window.location.href=location;
                });*/

                //提交导出终端业务流量分析表数据的导出任务
                $scope.exportTermBusTask = function(){
                    if(down == true){
                        var condition = {
                            starttime : $("#starttime1").val(),
                            endtime : $("#endtime1").val(),
                            brand : $scope.brand,
                            mainmodule:"终端多维分析",
                            submodule:"终端业务流量",
                            appname:"终端业务流量"
                        }
                        var url = httpservice.serverurl + "script/Terminal/SubmitTermBusTask";
                        submitDownloadTask(condition,url);
                    }else{
                        alert("无数据下载");
                        return false;
                    }
                }

                //提交导出终端业务流量用户明细的导出任务
                $(".serviceTypebtn").live("click",function(){
                    var condition={
                        brand:$(this).attr("brand"),
                        type:$(this).attr("type"),
                        network:$(this).attr("network"),
                        subservicetype:$(this).attr("subservice"),
                        starttime:$(this).attr("starttime"),
                        endtime:$(this).attr("endtime"),
                        mainmodule:"终端多维分析",
                        submodule:"终端业务流量",
                        appname:"终端业务流量"
                    };
                    var url = httpservice.serverurl + "script/Terminal/SubmitTermBusUserDetailTask";
                    submitDownloadTask(condition,url);
                });

                $scope.init = function(){
                    var date= $scope.getinitdate();
                    $("#starttime1").val(date.startdate);
                    $("#endtime1").val(date.enddate);
                };
                $scope.getterminalLen = function(condition){
                    $scope.$emit('loading', 'true');
                    var url = "terminalLen.do";
                    var terminalLen=httpservice.javaPost(url,condition,function(result){
                        $scope.grid1.gridpagedata.totalcount = result.recordCount;
                        $scope.grid1.gridpagedata.totalpage = result.pageCount;
                        $scope.grid1.gridpagedata.startline =result.startLine;
                        $scope.grid1.gridpagedata.endline = result.endLine;
                        $scope.grid1.gridpagedata.datacount = result.dataCount;
                        $scope.grid1.griddata.data = result.datas;
                        $scope.brand = result.brand;
                        $scope.endtime = result.endtime;
                        $scope.starttime = result.starttime;
                        if(result.dataCount>0){
                            down = true;
                        }
                        if(isini=="init"){
                            $scope.init();
                        }
                        $scope.$apply();
                    });
                    $.when(terminalLen).then(function () {
                    }).always(function () {
                            $scope.$emit('loading', 'false');
                            $scope.$apply();
                        });
                };


                $scope.dialogList={
                    title:"下载列表",
                    ok:function(){},
                    cancel:function(){}
                };

                $scope.downloadList = {
                    griddata: {
                        colnames: ['ID','下载文件名', '创建时间','主模块','子模块','应用名称', '下载状态', '操作'],
                        colmodel: [
                            { name: 'id', index: 'id', sortable: false, align:"center",hidden:true },
                            { name: 'filename', index: 'filename', sortable: false, align:"center" },
                            { name: 'createtime', index: 'createtime', sortable: false, align:"center" },
                            { name: 'mainmodule',width:100, index: 'mainmodule', sortable: false, align:"center"},
                            { name: 'submodule',width:100, index: 'submodule', sortable: false, align:"center"},
                            { name: 'appname',width:100, index: 'appname', sortable: false,sorttype: 'number', align:"center"},
                            { name: 'status',width:100, index: 'status', sortable: false,sorttype: 'number', align:"center"},
                            { name: 'operate', index: 'operate', sortable: false, align:"center",formatter:downloadoperate}
                        ],
                        data: [
                            { "filename": "微信", "mainmodule": 1000, "submodule":1000, "appname":1000, "createtime": 1000,"status":1000, "operate":1000}
                        ]
                    },
                    obj:{},
                    shrinkToFit:true,
                    gridresize: function (grid) {
                        grid.setGridWidth(800);
                        grid.setGridHeight(300);
                    }
                };

                $scope.showDownloadlist = function(){
                    $scope.dialogList.show();
                    $scope.getTermBusDownloadList();
                }

                $scope.getDownloadList = function(postdata){
                    $.ajax({
                        url:httpservice.serverurl + "script/Common/GetTaskList",
                        type:"post",
                        data:JSON.stringify(postdata),
                        dataType:"json",
                        success:function(result){
                            $scope.downloadList.griddata.data = result.datas;
                            $scope.$apply();
                        },
                        error:function(xmlHttpRequest,textStatus){
                            //alert("请求出错!");
                        },
                        complete:function(xmlhHttpRequest,textStatus){

                        }
                    });
                }

                $scope.getTermBusDownloadList = function(){
                    var condition = {
                        mainmodule:"终端多维分析",
                        submodule:"终端业务流量"
                    }
                    $scope.getDownloadList(condition);
                }

                //初始化
                $scope.init = function(){
                    $scope.getTermBusDownloadList();
                    setInterval(function(){
                        $scope.$apply($scope.getTermBusDownloadList);
                    }, 60*1000);
                };

                $scope.init();

            };
            angular.module('terminalAnalysis-ctrl', [])
                .controller('TerminalHotCtrl', ['$scope', 'HttpService', terminalHotCtrl])
                .controller('TerminalBusCtrl', ['$scope', 'HttpService', terminalBusCtrl]);
        })(jQuery);
    }
});